package cn.com.captureNetPacket.model;


import java.time.LocalDateTime;
import java.util.Map;

import cn.com.captureNetPacket.Analysis;
import cn.com.captureNetPacket.util.Tools;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class SignallingInfo {
		// sip的callid
		private String callId;
		// VOICE的 callid
		private String callUUID;
		private String fromUser;
		private String toUser;
		private String caller;
		private String called;
		private String deviceNumber;
		private String agentID;
		private LocalDateTime beginTime=LocalDateTime.now();
		private LocalDateTime ringTime;
		private LocalDateTime establishTime;
		private LocalDateTime endTime;
		private Map<String,String> appData;
		// 用户客户端挂机
		private String endCase;
		private String clientIP;
		private String clientMAC;
		private String serverIP;
		//呼叫方向，0:呼出,1:呼入
		private String direction="out";
		
		// 设置关联录音的路径
		private String recordPath;
		
		public String toStringPart() {
			return "SignallingInfo [callId=" + callId + ", from=" + fromUser + ", to=" + toUser + ", beginTime=" + beginTime+ ", deviceNumber=" 
		+ deviceNumber+ ", clientIP=" + clientIP+ ", serverIP=" + serverIP
					+ ", direction=" + direction+ "]";
		}
			/**
			 * 
				* <strong>Title: <strong>
				* <p>Description:  rtp流结束时清理 <p>
				* <p>Company: </p> 
				*<strong>Copyright:</strong> Copyright (c) 2019
				* @version: 1.0
				* @author  ASUS:augustWei
				* @date  2020 下午10:23:32
			 */
			public void clear(NetworkInfo networkInfo){
				try {
					if(callId!=null)
				 if(Analysis.callSignallingInfos.containsKey(callId))
				Analysis.callSignallingInfos.remove(callId);
				 if(Analysis.clientSignallingCallInfos.containsKey(networkInfo.getSourceIP()+">"+networkInfo.getSourcePort()))
				Analysis.clientSignallingCallInfos.remove(networkInfo.getSourceIP()+">"+networkInfo.getSourcePort());
				 if(Analysis.clientSignallingCallInfos.containsKey(networkInfo.getDestinationIP()+">"+networkInfo.getDestinationPort()))
				Analysis.clientSignallingCallInfos.remove(networkInfo.getDestinationIP()+">"+networkInfo.getDestinationPort());
				}catch (Exception e) {
					log.error("clear: {}",networkInfo,e);
				}
			}
			
			
			public SignallingInfoDto toSignallingInfoDto() {
				SignallingInfoDto signallingInfoDto=new SignallingInfoDto();
				signallingInfoDto.setCallId(callId);
				if(callUUID!=null && callUUID.trim().length()>0)
					signallingInfoDto.setCallUUID(callUUID);
				if(fromUser!=null && fromUser.trim().length()>0)
					signallingInfoDto.setFromUser(fromUser);
				if(toUser!=null && toUser.trim().length()>0)
					signallingInfoDto.setToUser(toUser);
				if(caller!=null && caller.trim().length()>0)
					signallingInfoDto.setCaller(caller);
				if(called!=null && called.trim().length()>0)
					signallingInfoDto.setCalled(called);
				if(deviceNumber!=null && deviceNumber.trim().length()>0)
					signallingInfoDto.setDeviceNumber(deviceNumber);
				if(agentID!=null && agentID.trim().length()>0)
					signallingInfoDto.setAgentID(agentID);
				if(beginTime!=null)
					signallingInfoDto.setBeginTime(beginTime.format(Tools.dfFull));
				if(ringTime!=null)
					signallingInfoDto.setRingTime(ringTime.format(Tools.dfFull));
				if(establishTime!=null)
					signallingInfoDto.setEstablishTime(establishTime.format(Tools.dfFull));
				if(endTime!=null)
					signallingInfoDto.setEndTime(endTime.format(Tools.dfFull));
				if(appData!=null && appData.size()>0)
					signallingInfoDto.setAppData(appData);
				if(endCase!=null && endCase.trim().length()>0)
					signallingInfoDto.setEndCase(endCase);
				if(clientIP!=null && clientIP.trim().length()>0)
					signallingInfoDto.setClientIP(clientIP);
				if(serverIP!=null && serverIP.trim().length()>0)
					signallingInfoDto.setServerIP(serverIP);
				if(clientMAC!=null && clientMAC.trim().length()>0)
					signallingInfoDto.setClientMAC(clientMAC);
				if(direction!=null && direction.trim().length()>0)
					signallingInfoDto.setDirection(direction);
				if(recordPath!=null && recordPath.trim().length()>0)
					signallingInfoDto.setRecordPath(recordPath);
				
				return signallingInfoDto;
			}
			
}
